package st.business.tepth.business.goodsreceive;

import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.business.bean.MaterialWaitApplyBean;
import st.business.tepth.business.bean.PickGoods;
import st.business.tepth.business.bean.post.IDBean;
import st.business.tepth.business.goodsreceive.entity.GoodsReceiveLevel0;
import st.business.tepth.business.goodsreceive.entity.GoodsReceiveLevel1;

/**
 * @author zhangyu
 * @date 2019/2/23
 */
public class GoodsReceiveAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    private List<MaterialWaitApplyBean> mCheckedList = new ArrayList<>();
    private OnItemCheckAllListener mCheckAllListener;
    private int mLevel0Count = 0;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public GoodsReceiveAdapter(List<MultiItemEntity> data,
                               OnItemCheckAllListener listener) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_goods_receive_lv_0);
        addItemType(TYPE_LEVEL_1, R.layout.item_goods_receive_lv_1);
        mCheckAllListener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_LEVEL_0:
                final GoodsReceiveLevel0 lv0 = (GoodsReceiveLevel0) item;
                final MaterialWaitApplyBean goodsOrderBean = lv0.getGoodsOrderBean();
                helper.setChecked(R.id.check_all, lv0.isCheck());
                helper.getView(R.id.layout_checkbox).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("layout_checkbox", "onClick");
                        helper.setChecked(R.id.check_all, !lv0.isCheck());
                        lv0.setCheck(!lv0.isCheck());
                        if (lv0.isCheck()) {
                            mCheckedList.add(goodsOrderBean);
                        } else {
                            mCheckedList.remove(goodsOrderBean);
                        }
                        if (mCheckAllListener != null) {
                            mCheckAllListener.onCheckAll(
                                    mCheckedList.size() == mLevel0Count, mCheckedList);
                        }
                    }
                });
                helper.setText(R.id.tv_apply_time, goodsOrderBean.getGmtCreate());
                helper.setText(R.id.tv_bus_number, goodsOrderBean.getVehicleNumber()
                ==null?"无":goodsOrderBean.getVehicleNumber());
                helper.setText(R.id.tv_apply_goods, goodsOrderBean.getMaterialName());
                helper.setText(R.id.tv_type, getType(goodsOrderBean.getType()));
                helper.setText(R.id.tv_materials_state, goodsOrderBean.getInventory()
                        == 0 ? "无货" : "有货");
                helper.getView(R.id.tv_apply_goods).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_LEVEL_1:
                GoodsReceiveLevel1 level1 = (GoodsReceiveLevel1) item;
                PickGoods goodsBean = level1.getGoodsBean();
                helper.setText(R.id.tv_goods_name, goodsBean.getMaterialName());
                helper.setText(R.id.tv_brand, "品牌:" + goodsBean.getBrand());
                helper.setText(R.id.tv_version, "型号:" + goodsBean.getModelNo());
                helper.setText(R.id.tv_specification, "规格:" + goodsBean.getSpecifications());
                helper.setText(R.id.tv_batch, "批次号:" + goodsBean.getBatchNo());
                helper.setText(R.id.tv_shelf_number, "自编号:" + goodsBean.getShelfNo());
                helper.setText(R.id.tv_count, goodsBean.getValue() + goodsBean.getUnit());
                break;
        }
    }

    void checkAll(boolean isCheckAll) {
        if (mCheckedList != null) {
            mCheckedList.clear();
        }
        List<MultiItemEntity> list = getData();
        if (!list.isEmpty()) {
            for (MultiItemEntity entity : list) {
                if (entity.getItemType() == TYPE_LEVEL_0) {
                    GoodsReceiveLevel0 level0 = (GoodsReceiveLevel0) entity;
                    level0.setCheck(isCheckAll);
                    if (isCheckAll) {
                        mCheckedList.add(level0.getGoodsOrderBean());
                    }
                }
            }
            notifyDataSetChanged();
        }
    }

    public void getLevel0Count() {
        List<MultiItemEntity> list = getData();
        for (MultiItemEntity entity : list) {
            if (entity.getItemType() == TYPE_LEVEL_0) {
                mLevel0Count++;
            }
        }
    }

    public String getType(int type) {
        String str = "耗材";
        switch (type) {
            case 1:
                str = "小修";
                break;
            case 2:
                str = "一级保养";
                break;
            case 3:
                str = "二级保养";
                break;
            case 4:
                str = "三级保养";
                break;
        }
        return str;
    }

    public List<MaterialWaitApplyBean> getCheckedList() {
        return mCheckedList;
    }

    public List<IDBean> getSelectGoods() {
        List<IDBean> idBeans = new ArrayList<>();
        if (mCheckedList != null) {
            for (MaterialWaitApplyBean waitApplyBean : mCheckedList) {
                List<PickGoods> pickGoods = waitApplyBean.getParts();
                if (pickGoods != null) {
                    for (PickGoods goods : pickGoods) {
                        idBeans.add(new IDBean(goods.getId()));
                    }
                }
            }
        }
        return idBeans;
    }

    public interface OnItemCheckAllListener {
        public void onCheckAll(boolean isCheckAll, List<MaterialWaitApplyBean> list);
    }

}
